﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Vds2465Telegrams;

namespace Vds2465Parser
{
	internal static class GenParse_UserData_60_NetStatus
	{
		#region private constants

		private const int NrOfBytes = 3;

		#endregion private constants

		internal static byte[] GenerateBytes(UserData userData)
		{
			byte[] userDataBytes = null;

			if (userData is UserData_60_NetStatus)
			{
				UserData_60_NetStatus userData60 = userData as UserData_60_NetStatus;
				userDataBytes = new byte[NrOfBytes];
				userDataBytes[0] = NrOfBytes - GenParse_UserData.HeaderLength;	// user data length
				userDataBytes[1] = UserData.UserData60NetStatus;
				userDataBytes[2] = GenParse_NetStatus.GenerateByte(userData60.NetStatus);
			}			
			else
			{
				// TODO handle error
			}

			return userDataBytes;
		}

		internal static bool ParseBytes(byte[] userDataBytes, out UserData userData60, out int consumedBytes)
		{
			userData60 = null;
			consumedBytes = 0;

			if (userDataBytes.Length >= NrOfBytes)
			{
				// first byte
				if (userDataBytes[0] == NrOfBytes - GenParse_UserData.HeaderLength)					
				{
					if (userDataBytes[1] == UserData.UserData60NetStatus)
					{
						byte netStatusByte = userDataBytes[2];
						eNetStatus netStatus;
						bool bRetVal = GenParse_NetStatus.ParseByte(netStatusByte, out netStatus);
						if (bRetVal)
						{
							userData60 = new UserData_60_NetStatus(netStatus);
							consumedBytes = NrOfBytes;
							return true;							
						}						

						// TODO handle error - wrong value for the ErrorCode
						return false;
					}

					// TODO handle error - wrong value for the data type
					return false;
				}

				// TODO handle error (wrong value in first byte the length)
				return false;
			}

			// TODO handle error (not enough bytes)
			return false;
		}
	}
}
